package com.code.app.activity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.res.AssetManager;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Parcel;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.code.app.R;
import com.code.app.base.BaseActivity;

public class AnimActivity extends BaseActivity {

    private static final String TAG = "AnimActivity";

    private ImageView imageView;

    @Override
    public int getLayoutId() {
        return R.layout.activity_anim;
    }

    @Override
    public void initFunction(Bundle savedInstanceState) {
        imageView = findViewById(R.id.img);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_translate:
                translate(imageView);
                break;
            case R.id.btn_gaoji:
                gaoji(imageView);
                break;
            case R.id.btn_object:
                objectAnim(imageView);
                break;
            case R.id.btn_animationset:
                animSet(imageView);
                break;
        }
    }






    private void animSet(View view) {
        ObjectAnimator objectAnimatorx = ObjectAnimator.ofFloat(view, "translationX", 0f, 200f, 0f);
        ObjectAnimator objectAnimatory = ObjectAnimator.ofFloat(view, "translationY", 0f, 200f, 0f);
        ObjectAnimator objectAnimatorScale = ObjectAnimator.ofFloat(view, "rotationX", 0f, 360f, 0);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(5000);
//        animatorSet.playTogether(objectAnimatorx, objectAnimatory, objectAnimatorScale);
        animatorSet.playSequentially(objectAnimatorx, objectAnimatorScale, objectAnimatory);
        animatorSet.start();
    }

    private void objectAnim(View view) {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f, 1f);
        objectAnimator.setDuration(5000);
        objectAnimator.start();
    }

    private void translate(final View imageView) {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(1f, 0f, 1f);
//        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 300f, 0);
        valueAnimator.setDuration(5000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {


            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                long time = animation.getCurrentPlayTime();
                Log.i(TAG, "time is " + time);
                float postiony = (float) animation.getAnimatedValue();
//                imageView.setTranslationY(postion);
                imageView.setScaleX(postiony);
                imageView.setScaleY(postiony);
            }
        });
        valueAnimator.start();

//        		/**
//		 * x:匀速
//		 * y:加速度   y=1/2*g*t*t
//		 * 使用估值器最好实现。
//		 */
//		ValueAnimator valueAnimator1= new ValueAnimator();
//        valueAnimator1.setDuration(4000);
////		valueAnimator1.setFloatValues(values)
//        valueAnimator1.setObjectValues(new PointF(0, 0));
//		//估值器---定义计算规则
//        valueAnimator1.setEvaluator(new TypeEvaluator<PointF>() {
//
//			@Override
//			public PointF evaluate(float fraction, PointF startValue,
//                                   PointF endValue) {
//				//拿到每一个时间点的坐标
//				//x=v*t (s秒)
//				PointF pointF = new PointF();
//				pointF.x = 100f*(fraction*4);//初始速度*(执行的百分比*4)
////				pointF.y = 0.5f*9.8f*(fraction*4)*(fraction*4);
//				pointF.y = 0.5f*150f*(fraction*4)*(fraction*4);
//				return pointF;
//			}
//		});
//
//        valueAnimator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//
//			@Override
//			public void onAnimationUpdate(ValueAnimator animation) {
//				//得到此时间点的坐标
//				PointF pointF = (PointF) animation.getAnimatedValue();
//
//				imageView.setX(pointF.x);
//				imageView.setY(pointF.y);
//			}
//		});
//        valueAnimator1.start();
    }


    private void gaoji(final View view) {
        Point point1 = new Point();
        point1.x = 0;
        point1.y = 0;
        Point point2 = new Point();
        point2.x = 0;
        point2.y = 300;
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new MyTypeEvaluator(), point1, point2);
        valueAnimator.setDuration(5000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {


            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                long time = animation.getCurrentPlayTime();
                Log.i(TAG, "time is " + time);
                Point point = (Point) animation.getAnimatedValue();
                Log.i(TAG, "point y is " + point.y);
                view.setTranslationY(point.y);
            }
        });
        valueAnimator.start();
    }

    class MyTypeEvaluator implements TypeEvaluator<Point> {
        @Override
        public Point evaluate(float fraction, Point startValue, Point endValue) {
            int xd = Math.abs(endValue.x - startValue.x);
            int yd = Math.abs(endValue.y - startValue.y);
            Point point = new Point();
            point.x = (int) (xd * fraction);
            point.y = (int) (yd * fraction);
            return point;
        }
    }

}
